﻿using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Dotnet.Utils.Utility.CollectionUtil
{
    public static class RegexUtil
    {
        public static Dictionary<string, string> GetValues(string input, string pattern, string[] resultPatterns, RegexOptions options = RegexOptions.IgnoreCase)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            Dictionary<string, string> result;
            if (string.IsNullOrWhiteSpace(input))
            {
                result = dictionary;
            }
            else
            {
                Match match = Regex.Match(input, pattern, options);
                if (!match.Success)
                {
                    result = dictionary;
                }
                else
                {
                    AddResults(dictionary, match, resultPatterns);
                    result = dictionary;
                }
            }
            return result;
        }

        private static void AddResults(Dictionary<string, string> result, Match match, string[] resultPatterns)
        {
            if (resultPatterns == null)
            {
                result.Add(string.Empty, match.Value);
            }
            else
            {
                foreach (string text in resultPatterns)
                {
                    result.Add(text, match.Result(text));
                }
            }
        }

        public static string GetValue(string input, string pattern, string resultPattern = "", RegexOptions options = RegexOptions.IgnoreCase)
        {
            string result;
            if (string.IsNullOrWhiteSpace(input))
            {
                result = string.Empty;
            }
            else
            {
                Match match = Regex.Match(input, pattern, options);
                if (!match.Success)
                {
                    result = string.Empty;
                }
                else
                {
                    result = string.IsNullOrWhiteSpace(resultPattern) ? match.Value : match.Result(resultPattern);
                }
            }
            return result;
        }

        public static string[] Split(string input, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
        {
            string[] result;
            if (string.IsNullOrWhiteSpace(input))
            {
                result = new string[0];
            }
            else
            {
                result = Regex.Split(input, pattern, options);
            }
            return result;
        }

        public static string Replace(string input, string pattern, string replacement, RegexOptions options = RegexOptions.IgnoreCase)
        {
            string result;
            if (string.IsNullOrWhiteSpace(input))
            {
                result = string.Empty;
            }
            else
            {
                result = Regex.Replace(input, pattern, replacement, options);
            }
            return result;
        }

        public static bool IsMatch(string input, string pattern)
        {
            return IsMatch(input, pattern, RegexOptions.IgnoreCase);
        }

        public static bool IsMatch(string input, string pattern, RegexOptions options)
        {
            return Regex.IsMatch(input, pattern, options);
        }
    }
}